package com.example.teamtribe.main.club.clubintroduce.media;

import android.os.Bundle;
import android.util.Log;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.CallbackStore;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.database.Media;
import com.example.teamtribe.json.JsonMedia;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;
import com.example.teamtribe.utils.Utility;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by lenovo on 2018/5/6.
 */

public class MediaModel {
    private static MediaModel instance;
    private MediaModel() {}
    public static MediaModel getInstance() {
        if (instance == null) {
            synchronized (MediaModel.class) {
                if (instance == null) {
                    instance = new MediaModel();
                }
            }
        }
        return instance;
    }
    /*
    TeamTribeToken string
    name string 长度1~16
    teamId string
    cover string封面的文件名，长度32
    description string相册描述，长度0~45
    type string1相册2影集
     */
    public void addMedia(String name,int teamId,String cover,String description,int type,final Back back){
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", Utility.getToken());
        params.put("name", name);
        params.put("teamId", teamId+"");
        params.put("cover", cover);
        params.put("description", description);
        params.put("type", type+"");
        HttpUtil.post(URLStore.TEAM_MEDIA_ADD, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("回调id", str);
                try {
                    JSONObject object = new JSONObject(str);
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.failure(HttpUtil.getCodeMeg(status));
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putInt("id", object.getInt("albumId"));
                        back.success(bundle);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void deleteMedia(String token,int teamId,int albumId,final Back back){
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", token);
        params.put("albumId", albumId+"");
        params.put("teamId", teamId+"");
        HttpUtil.post(URLStore.TEAM_MEDIA_DELETE, params, CallbackStore.getCallback(back) );
    }

    public void updateMedia(int mediaId,String name,int teamId,String cover,String description,int type,final Back back){
        Map<String, String> params = new HashMap<>();
        params.put("TeamTribeToken", Utility.getToken());
        params.put("name", name);
        params.put("teamId", teamId+"");
        params.put("cover", cover);
        params.put("description", description);
        params.put("type", type+"");
        params.put("id", mediaId+"");
        HttpUtil.post(URLStore.TEAM_MEDIA_UPDATE, params, CallbackStore.getCallback(back));
//            @Override
//            public void onFailure(Call call, IOException e) {{
//                back.failure(HttpUtil.getFailureMeg());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String str = response.body().string();
//                try {
//                    JSONObject object = new JSONObject(str);
//                    int status = object.getInt("code");
//                    if (status != 0) {
//                        back.failure(HttpUtil.getCodeMeg(status));
//                    } else {
//                        Bundle bundle = new Bundle();
//                        //bundle.putInt("id", object.getInt("albumId"));
//                        back.success(bundle);
//                    }
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }


    public void getMediaIds(int teamId,final Back back){
        Map<String, String> params = new HashMap<>();
        params.put("teamId", teamId + "");
        HttpUtil.post(URLStore.TEAM_MEDIA_GET, params, CallbackStore.getIdsCallback(back));
    }

    public void getMedia(final int mediaId, final FileModel.FileBack back) {
        Map<String, String> params = new HashMap<>();
        params.put("albumId", mediaId + "");
        HttpUtil.post(URLStore.TEAM_MEDIAINFO_GET, params, new CallbackStore.CallbackImpl<FileModel.FileBack>(back) {
            @Override
            public void onResponse(String json) {
                Log.d("多媒体", json);
                JsonMedia jsonMedia = new Gson().fromJson(json, JsonMedia.class);
                final int status = jsonMedia.getCode();
                if (status != 0) {
                    back.failure(HttpUtil.getCodeMeg(status));
                } else {
                    final Media media = jsonMedia.getMedia();
                    Bundle b = new Bundle();
                    b.putParcelable("media", media);
                    back.success(b);
                    FileModel.getInstance().download(media.getCover(), 0, back);
                }
            }
        });
    }

    public List<Media> getLocalMedias(int teamId,int type) {
        return DataSupport.where("teamId = ? and type = ?", teamId + "",type+"").find(Media.class);
    }
    public void clearLocalMedias(int type) {
        DataSupport.deleteAll(Media.class,"type=?",type+"");
    }
}
